﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChatWindow.aspx.cs" Inherits="ChatForum.Client.ChatWindow" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link href="Styles/siteLayoutStyle.css" rel="stylesheet" type="text/css" />
    <title></title>
</head>
<body>
    <form action="/" method="post" runat="server">
    <asp:ScriptManager runat="server" EnablePageMethods="true" />
    <div style="margin-left: 5px; margin-right: 5px; height: 450px;">
        <div id="chatOutputDiv" class="borderStyle" style="width: 97%; height: 400px; background-color: White; padding: 5px; margin-bottom: 10px; overflow: auto; ">
        </div>
            <asp:TextBox style="resize: none;" ID="chatInput" Width="100%" Height="50" TextMode="MultiLine" runat="server"
                onkeypress="ChatWindow.KeyInteract(event)" />
    </div>
    </form>
    <script type="text/javascript">
        window.onload = function PageLoad() {
            document.title = window.name;
            document.getElementById('<%=chatInput.ClientID %>').focus();
            window.opener.Chat.TrackOpening(window.name, window);
            setInterval(ChatWindow.Refresh, 1000);
        }
        window.onbeforeunload = function PageUnload() {
            window.opener.Chat.TrackClosing(window.name);
        }
        var ChatWindow = function () {
            function SendMessage() {
                PageMethods.OnPing(window.name, document.getElementById('<%=chatInput.ClientID %>').value, SendMessageSuccess, SendMessageFailed);
            }
            function SendMessageSuccess(result) {
                document.getElementById('<%=chatInput.ClientID %>').value = '';
            }
            function SendMessageFailed(error, userContext, methodName) {
                alert("Ping Failed");
            }
            function RefreshSuccess(result) {

                var chatOutputDiv = document.getElementById("chatOutputDiv");
                chatOutputDiv.innerHTML = '';

                var table = document.createElement("table");
                var tableBody = document.createElement("tbody");
                table.appendChild(tableBody);

                for (var i = 0; i < result.length; i++) {
                    var row = document.createElement("tr");
                    var senderColum = document.createElement("td");
                    senderColum.appendChild(document.createTextNode(result[i].Sender));
                    row.appendChild(senderColum);

                    var textColum = document.createElement("td");
                    textColum.appendChild(document.createTextNode(result[i].Text));
                    row.appendChild(textColum);

                    tableBody.appendChild(row);
                }

                chatOutputDiv.appendChild(table);
            }
            function RefreshFailed(error, userContext, methodName) {
                alert("Refreshed Failed");
            }
            return {
                KeyInteract: function (event) {
                    if (event.keyCode == 13 && !event.shiftKey) {
                        SendMessage();
                    } else if (event.keyCode == 27) {
                        window.close();
                    }
                },
                Refresh: function () {
                    PageMethods.OnRefresh(window.name, RefreshSuccess, RefreshFailed);
                }
            }
        } ();
        function KeyInteract(event) {
        }
    </script>
</body>
</html>
